﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using KidsGest.Models;
using System.Web.Helpers;

namespace KidsGest.Controllers
{ 
    public class PreRegistoController : Controller
    {
        private KidsGestEntities db = new KidsGestEntities();

        //
        // GET: /PreRegisto/

        public ViewResult Index()
        {
            

            return View(db.PreRegistos.ToList());
        }

        //
        // GET: /PreRegisto/Details/5

        public ViewResult Details(int id)
        {
            PreRegistoModel preregistomodel = db.PreRegistos.Find(id);
            return View(preregistomodel);
        }

        //
        // GET: /PreRegisto/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /PreRegisto/Create

        [HttpPost]
        public ActionResult Create(PreRegistoModel preregistomodel)
        {
             DateTime dataMin = DateTime.Now.AddYears(-5);
             DateTime dataMax = DateTime.Now.AddYears(-3);

            if (ModelState.IsValid)
            {

                var crianca = db.PreRegistos.Where(preregisto => preregisto.Email.ToLower() == preregistomodel.Email.ToLower()).Select(aluno => aluno.AlunoID);
                if (!crianca.Any())
                {
                    if (preregistomodel.DataNascimento >= dataMin && preregistomodel.DataNascimento <= dataMax)
                    {
                        db.PreRegistos.Add(preregistomodel);
                        db.SaveChanges();
                        SendEmail.SendEmailPreRegisto(preregistomodel.NomePai, preregistomodel.Email);
                        return RedirectToAction("PreRegistoSuccess");
                    }
                    else
                    {
                        ModelState.AddModelError("DataNascimento", "Pré-Inscrições só são aceites para crianças com idades compreendidas entre os 3 e os 5 anos.");
                    }
                }
                else
                {
                    ModelState.AddModelError("Email", "O e-mail introduzido já se encontra em uso.");
                }

            }

            return View(preregistomodel);
        }
        
        //
        // GET: /PreRegisto/Edit/5
 
        public ActionResult Edit(int id)
        {
            PreRegistoModel preregistomodel = db.PreRegistos.Find(id);
            return View(preregistomodel);
        }

        //
        // POST: /PreRegisto/Edit/5

        [HttpPost]
        public ActionResult Edit(PreRegistoModel preregistomodel)
        {
            if (ModelState.IsValid)
            {
                db.Entry(preregistomodel).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(preregistomodel);
        }

        //
        // GET: /PreRegisto/Delete/5
 
        public ActionResult Delete(int id)
        {
            PreRegistoModel preregistomodel = db.PreRegistos.Find(id);
            return View(preregistomodel);
        }

        //
        // POST: /PreRegisto/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {            
            PreRegistoModel preregistomodel = db.PreRegistos.Find(id);
            db.PreRegistos.Remove(preregistomodel);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }

        //
        // GET: /Account/ChangePasswordSuccess

        public ActionResult PreRegistoSuccess()
        {
            return View();
        }  
    }
}